package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// ConversationMember holds the schema definition for the ConversationMember entity.
type ConversationMember struct {
	ent.Schema
}

// Fields of the ConversationMember.
func (ConversationMember) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Immutable().
			Comment("会话成员ID"),
		field.Int64("conversation_id").
			Comment("会话ID"),
		field.Int64("user_id").
			Comment("用户ID"),
		field.Enum("role").
			Values("member", "admin", "owner").
			Default("member").
			Comment("成员角色"),
		field.Time("joined_at").
			Default(time.Now).
			Immutable().
			Comment("加入时间"),
		field.Int64("last_read_message_id").
			Optional().
			Comment("最后已读消息ID"),
		field.Bool("is_muted").
			Default(false).
			Comment("是否静音"),
		field.Bool("is_pinned").
			Default(false).
			Comment("是否置顶"),
	}
}

// Edges of the ConversationMember.
func (ConversationMember) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("conversation", Conversation.Type).
			Ref("members").
			Field("conversation_id").
			Unique().
			Required().
			Comment("所属会话"),
		edge.From("user", User.Type).
			Ref("memberships").
			Field("user_id").
			Unique().
			Required().
			Comment("成员用户"),
		edge.From("last_read_message", Message.Type).
			Ref("read_by_members").
			Field("last_read_message_id").
			Unique().
			Comment("最后已读消息"),
	}
}

// Indexes of the ConversationMember.
func (ConversationMember) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("conversation_id", "user_id").
			Unique(),
		index.Fields("user_id"),
	}
}

// Annotations of the ConversationMember.
func (ConversationMember) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "conversation_members",
		},
	}
}
